﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GamePanel : MonoBehaviour
{
    private Button btnPause;
    private Button btnPlay;
    private Text txtScore;
    private Text txtDiamondCount;


    // Start is called before the first frame update
    void Start()
    {
        btnPause = transform.Find("btn_Pause").GetComponent<Button>();
        btnPause.onClick.AddListener(OnPauseButtonClick);
        btnPlay = transform.Find("btn_Play").GetComponent<Button>();
        btnPlay.onClick.AddListener(OnPlayButtonClick);
        txtScore = transform.Find("txt_Score").GetComponent<Text>();
        txtDiamondCount = transform.Find("Diamond/txt_DiamondCount").GetComponent<Text>();

        gameObject.SetActive(false);
        btnPlay.gameObject.SetActive(false);

        EventCenter.AddListener(EventType.ShowGamePanel, Show);
        EventCenter.AddListener<int>(EventType.UpdateScore, UpdateScoreText);
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventType.ShowGamePanel, Show);
        EventCenter.RemoveListener<int>(EventType.UpdateScore, UpdateScoreText);
    }

    private void Show()
    {
        gameObject.SetActive(true);

    }

    private void OnPauseButtonClick()
    {
        btnPlay.gameObject.SetActive(true);
        btnPause.gameObject.SetActive(false);

        Time.timeScale = 0;
        GameManager.Instance.IsPause = true;
    }

    private void OnPlayButtonClick()
    {
        btnPause.gameObject.SetActive(true);
        btnPlay.gameObject.SetActive(false);

        Time.timeScale = 1;
        GameManager.Instance.IsPause = false;
    }

    private void UpdateScoreText(int score)
    {
        txtScore.text = score.ToString();
    }

}
